package com.ruoyi.ycl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.domain.BsDataReport;
import com.ruoyi.syrw.mapper.BsDataReportMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.ycl.mapper.BsSupMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ycl.domain.bo.BsJctzBo;
import com.ruoyi.ycl.domain.vo.BsJctzVo;
import com.ruoyi.ycl.domain.BsJctz;
import com.ruoyi.ycl.mapper.BsJctzMapper;
import com.ruoyi.ycl.service.IBsJctzService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 原材料进场台账Service业务层处理
 *
 * @author zyb
 * @date 2024-06-05
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class BsJctzServiceImpl implements IBsJctzService {

    private final BsJctzMapper baseMapper;
    private final SysDeptMapper deptMapper;
    private final BsSupMapper supMapper;
    private final ISysDictDataService dictDataService;
    private final BsDataReportMapper dataReportMapper;

    /**
     * 查询原材料进场台账
     */
    @Override
    public BsJctzVo queryById(Long tzId){
        BsJctzVo bsJctzVo = baseMapper.selectVoById(tzId);
        if (ObjUtil.isNotNull(bsJctzVo.getDeptId())) {
            bsJctzVo.setDeptName(deptMapper.selectVoById(bsJctzVo.getDeptId()).getDeptName());
        }
        if (ObjUtil.isNotNull(bsJctzVo.getSupId())) {
            bsJctzVo.setSupName(supMapper.selectVoById(bsJctzVo.getSupId()).getSupName());
        }
        if (ObjUtil.isNotNull(bsJctzVo.getInNum()) && StrUtil.isNotEmpty(bsJctzVo.getInUnit())) {
            bsJctzVo.setJincsl(BigDecimalUtil.stpTrailZero(bsJctzVo.getInNum()).toPlainString() + dictDataService.selectDictLabel("unit", bsJctzVo.getInUnit()));
        }
        if (ObjUtil.isNotNull(bsJctzVo.getDaibNum()) && StrUtil.isNotEmpty(bsJctzVo.getDaibUnit())) {
            bsJctzVo.setDaibsl(BigDecimalUtil.stpTrailZero(bsJctzVo.getDaibNum()).toPlainString() + dictDataService.selectDictLabel("unit", bsJctzVo.getDaibUnit()));
        }
        if (StrUtil.isNotEmpty(bsJctzVo.getClType())) {
            bsJctzVo.setClTypeLabel(dictDataService.selectDictLabel("cl_type", bsJctzVo.getClType()));
        }
        // 计算被试验次数
        Long count = dataReportMapper.selectCount(new LambdaQueryWrapper<BsDataReport>()
            .eq(BsDataReport::getTzId, tzId));
        bsJctzVo.setSyTimes(count);
        return bsJctzVo;
    }

    /**
     * 查询原材料进场台账列表
     */
    @Override
    public TableDataInfo<BsJctzVo> queryPageList(BsJctzBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BsJctz> lqw = buildQueryWrapper(bo);
        Page<BsJctzVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(v->{
            if (ObjUtil.isNotNull(v.getDeptId())) {
                v.setDeptName(deptMapper.selectVoById(v.getDeptId()).getDeptName());
            }
            if (ObjUtil.isNotNull(v.getSupId())) {
                v.setSupName(supMapper.selectVoById(v.getSupId()).getSupName());
            }
            if (ObjUtil.isNotNull(v.getInNum()) && StrUtil.isNotEmpty(v.getInUnit())) {
                v.setJincsl(BigDecimalUtil.stpTrailZero(v.getInNum()).toPlainString() + dictDataService.selectDictLabel("unit", v.getInUnit()));
            }
            if (ObjUtil.isNotNull(v.getDaibNum()) && StrUtil.isNotEmpty(v.getDaibUnit())) {
                v.setDaibsl(BigDecimalUtil.stpTrailZero(v.getDaibNum()).toPlainString() + dictDataService.selectDictLabel("unit", v.getDaibUnit()));
            }
            if (StrUtil.isNotEmpty(v.getClType())) {
                v.setClTypeLabel(dictDataService.selectDictLabel("cl_type", v.getClType()));
            }
            // 计算被试验次数
            Long count = dataReportMapper.selectCount(new LambdaQueryWrapper<BsDataReport>()
                .eq(BsDataReport::getTzId, bo.getTzId()));
            v.setSyTimes(count);
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询原材料进场台账列表
     */
    @Override
    public List<BsJctzVo> queryList(BsJctzBo bo) {
        LambdaQueryWrapper<BsJctz> lqw = buildQueryWrapper(bo);
        List<BsJctzVo> bsJctzVos = baseMapper.selectVoList(lqw);
        bsJctzVos.forEach(v->{
            if (ObjUtil.isNotNull(v.getDeptId())) {
                v.setDeptName(deptMapper.selectVoById(v.getDeptId()).getDeptName());
            }
            if (ObjUtil.isNotNull(v.getSupId())) {
                v.setSupName(supMapper.selectVoById(v.getSupId()).getSupName());
            }
            if (ObjUtil.isNotNull(v.getInNum()) && StrUtil.isNotEmpty(v.getInUnit())) {
                v.setJincsl(BigDecimalUtil.stpTrailZero(v.getInNum()).toPlainString() + dictDataService.selectDictLabel("unit", v.getInUnit()));
            }
            if (ObjUtil.isNotNull(v.getDaibNum()) && StrUtil.isNotEmpty(v.getDaibUnit())) {
                v.setDaibsl(BigDecimalUtil.stpTrailZero(v.getDaibNum()).toPlainString() + dictDataService.selectDictLabel("unit", v.getDaibUnit()));
            }
        });
        return bsJctzVos;
    }

    private LambdaQueryWrapper<BsJctz> buildQueryWrapper(BsJctzBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsJctz> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, BsJctz::getDeptId, bo.getDeptId());
        lqw.eq(bo.getClType() != null, BsJctz::getClType, bo.getClType());
        lqw.eq(StringUtils.isNotBlank(bo.getGuigxh()), BsJctz::getGuigxh, bo.getGuigxh());
        lqw.eq(bo.getSupId() != null, BsJctz::getSupId, bo.getSupId());
        lqw.eq(bo.getInDate() != null, BsJctz::getInDate, bo.getInDate());
        lqw.eq(StringUtils.isNotBlank(bo.getCunfwz()), BsJctz::getCunfwz, bo.getCunfwz());
        lqw.eq(StringUtils.isNotBlank(bo.getPih()), BsJctz::getPih, bo.getPih());
        lqw.eq(bo.getChucDate() != null, BsJctz::getChucDate, bo.getChucDate());
        lqw.eq(StringUtils.isNotBlank(bo.getTzStatus()), BsJctz::getTzStatus, bo.getTzStatus());
        lqw.orderByDesc(BsJctz::getInDate);
        return lqw;
    }

    /**
     * 新增原材料进场台账
     */
    @Override
    public Boolean insertByBo(BsJctzBo bo) {
        BsJctz add = BeanUtil.toBean(bo, BsJctz.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTzId(add.getTzId());
        }
        return flag;
    }

    /**
     * 修改原材料进场台账
     */
    @Override
    public Boolean updateByBo(BsJctzBo bo) {
        BsJctz update = BeanUtil.toBean(bo, BsJctz.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BsJctz entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除原材料进场台账
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
